<template>
  <div>
    <el-dialog
      :title="obj.isadd ? '活动添加' : '活动编辑'"
      :visible.sync="obj.istrue"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="活动期限">
          <el-date-picker
            v-model="value1"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="change"
            v-if="obj.istrue"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类">
          <el-select
            @change="changeone(form.first_cateid)"
            v-model="form.first_cateid"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in onelist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            @change="changetwo(form.first_cateid, form.second_cateid)"
            v-model="form.second_cateid"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in twolist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选中商品">
          <el-select v-model="form.goodsid" placeholder="请选择活动区域">
            <el-option
              v-for="item in threelist"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isadd"
          >添 加</el-button
        >
        <el-button type="primary" @click="bianji" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import { mapActions, mapGetters } from "vuex";
import { goodslist, seckadd, seckinfo ,seckedit} from "../../../request/api";
import { successAlert, warningAlert } from "../../../util/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      dialogVisible: true,
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      value1: "",
      onelist: [],
      twolist: [],
      threelist: [],
    };
  },
  watch: {
    catelist: {
      deep: true,
      handler() {
        this.onelist = this.catelist;
        // console.log(this.onelist);
      },
    },
  },
  methods: {
    ...mapActions({
      reqsecklist: "seckill/reqchangelist",
      reqcatelist: "cate/reqchangelist",
      // reqgoodslist: "goods/reqchangelist",
    }),
    //   点击弹窗x执行的回调函数
    handleClose() {
      this.quxiao();
    },
    // 一级类别改变时
    changeone(pid) {
      this.reqcatelist({ pid: pid });
      this.twolist = this.catelist.find(
        (item) => this.form.first_cateid == item.id
      ).children
        ? this.catelist.find((item) => this.form.first_cateid == item.id)
            .children
        : [];
      this.form.second_cateid = "";
    },
    // 二级类别改变时
    changetwo() {
      goodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        // console.log(res);
        this.threelist = res.data.list;
      });
    },
    // 时间改变时
    change(time) {
      this.form.begintime = new Date(time[0]).getTime();
      this.form.endtime = new Date(time[1]).getTime();
    },
    // 清空
    empty() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      this.onelist = [];
      this.twolist = [];
      this.threelist = [];
    },
    // 点击取消
    quxiao() {
      this.empty();
      this.obj.istrue = false;
      this.value1 = [];
    },
    // 点击添加
    add() {
      // console.log(this.form);
      seckadd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 成功的弹窗
          successAlert(res.data.msg);
          // 关闭当前对话框并清空对话框数据
          this.quxiao();
          // 重新请求列表数据
          this.reqsecklist();
          this.reqcatelist();
        } else {
          // 失败的弹窗
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条信息
    getinfo(id) {
      seckinfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.form.id=id
        this.value1 = [
          Number(this.form.begintime),
          Number(this.form.endtime),
        ];
        this.reqcatelist({ pid: this.form.first_cateid });
        this.twolist = this.catelist.find(
          (item) => this.form.first_cateid == item.id
        ).children
          ? this.catelist.find((item) => this.form.first_cateid == item.id)
              .children
          : [];

        goodslist({
          fid: this.form.first_cateid,
          sid: this.form.second_cateid,
        }).then((res) => {
          // console.log(res);
          this.threelist = res.data.list;
        });
      });
    },
    // 点击编辑
    bianji(){      
      // console.log(this.form);
      seckedit(this.form).then(res=>{
        if (res.data.code == 200) {
          // 成功的弹窗
          successAlert(res.data.msg);
          // 关闭当前对话框并清空对话框数据
          this.quxiao();
          // 重新请求列表数据
          this.reqsecklist();
          this.reqcatelist();
        } else {
          // 失败的弹窗
          warningAlert(res.data.msg);
        }
      })
    }
  },
  computed: {
    ...mapGetters({
      secklist: "seckill/list",
      catelist: "cate/list",
    }),
  },
  mounted() {
    this.reqsecklist();
    this.reqcatelist();
    // this.onelist = this.catelist;
  },
};
</script>

<style>
</style>